//
//  ByViewStoreSectionHeader.m
//  itboye_shop_ios
//
//  Created by pro on 17/2/7.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewStoreSectionHeader.h"

@implementation ByViewStoreSectionHeader
{
    UIButton        *_selectBtn;    // 选择按钮
    UIImageView     *_storeIcon;    // 店铺图标
    UILabel         *_storeLabel;   // 店铺label
    UIImageView     *_arrowView;    // 箭头
}
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
    
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    // 选中视图
    _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectBtn.frame = CGRectMake(0, 0, 40, self.height);
    _selectBtn.imageEdgeInsets = UIEdgeInsetsMake((self.height - 20) / 2, 10, (self.height - 20) / 2, 10);
    [_selectBtn setImage:[UIImage imageNamed:@"Common_unSelected"] forState:UIControlStateNormal];
    [_selectBtn setImage:[UIImage imageNamed:@"Common_selected"] forState:UIControlStateSelected];
    [_selectBtn addTarget:self action:@selector(select) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_selectBtn];
    
    _storeIcon = [[UIImageView alloc] initWithFrame:CGRectMake(_selectBtn.right + _selectBtn.left, (self.height - 20) / 2, 20, 20)];
    [self addSubview:_storeIcon];
    
    _storeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_storeIcon.right + 10, 0, 100, self.height)];
    _storeLabel.adjustsFontSizeToFitWidth = YES;
    _storeLabel.font = FONT(14);
    _storeLabel.textColor = [UIColor blackColor];
    _storeLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_storeLabel];
    
    _arrowView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    _arrowView.backgroundColor = [UIColor cyanColor];
    _arrowView.y = self.height / 2;
}

- (void)setStoreModel:(ByModelShopCar *)storeModel {
    
    _storeModel = storeModel;
    
    _selectBtn.selected = _storeModel.selected;
    
    // 测试数据
    [_storeIcon setImageWithURL:[NSURL URLWithString:_storeModel.store_logo] placeholderImage:DEFAULT_IMAGE];
    
    _storeLabel.text = _storeModel.store_name;
    _storeLabel.width = [_storeLabel.text getSizeOfString:14].width + 10;
    
    _arrowView.left = _storeLabel.right;
}

#pragma mark - 点击事件
- (void)select {
    
    _selectBtn.selected = !_selectBtn.selected;
    if (self.selectBlock) {
        self.selectBlock(_selectBtn.selected);
    }

}

@end
